package com.jimline.newRate.ui.main;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jimline.newRate.core.ResourceController;

public class MainWindow extends JFrame {
    private ArrayList<JTextField> m_incoming = new ArrayList<JTextField>();
    private ArrayList<JTextField> m_outgoing = new ArrayList<JTextField>();
    private Box m_inputBox;
    private FlowLayout m_buttonBox;
    private JButton m_btnCaculate;
    private JButton m_btnReset;
    
    public MainWindow()
    {	
    	JLabel title = new JLabel("本计算器适用于2011年9月1日新税法", JLabel.CENTER);
    	JPanel buttonPnl = new JPanel();
    	Container ctn = this.getContentPane();
    	
    	ctn.add(title, BorderLayout.NORTH);
    	
    	m_inputBox = Box.createHorizontalBox();
    	m_buttonBox = new FlowLayout();
    	buttonPnl.setLayout(m_buttonBox);
    	
    	m_inputBox.add(Box.createHorizontalStrut(10));
    	initIncomingItem();
    	m_inputBox.add(Box.createHorizontalStrut(20));
    	initOutgoingItem();
    	m_inputBox.add(Box.createHorizontalStrut(10));
    	initButtons();
    	
    	buttonPnl.add(m_btnCaculate);
    	buttonPnl.add(m_btnReset);
    	
    	ctn.add(m_inputBox, BorderLayout.CENTER);
    	ctn.add(buttonPnl, BorderLayout.SOUTH);
    	pack();
    	setSize(400, this.getHeight() + 5);
    	setResizable(false);
    }
    
    public void initIncomingItem()
    {
    	Box grpBox = Box.createVerticalBox();
    	
        JLabel title = new JLabel("收入项");
        grpBox.add(title);

        ArrayList<String> incomingList = ResourceController.instance().getIncomingItem();
        Iterator<String> i = incomingList.iterator();
        while(i.hasNext()) {
        	Box itemBox = Box.createHorizontalBox();
        	String strLbl = i.next();
            JLabel lbl = new JLabel(strLbl);
            JTextField line = new JTextField("0.0");
            line.addKeyListener(new TextKeyAdapter());
            line.addFocusListener(new TextFocusListener());
            
            m_incoming.add(line);
            
            itemBox.add(lbl);
            itemBox.add(line);
            grpBox.add(Box.createVerticalStrut(5));
            grpBox.add(itemBox);
        }
        
        m_inputBox.add(grpBox);
    }

    public void initOutgoingItem()
    {
    	Box grpBox = Box.createVerticalBox();
    	
        JLabel title = new JLabel("扣除项");
        grpBox.add(title);

        ArrayList<String> outgoingList = ResourceController.instance().getOutgointItem();
        Iterator<String> i = outgoingList.iterator();
        while(i.hasNext()) {
        	Box itemBox = Box.createHorizontalBox();
        	String strLbl = i.next();
            JLabel lbl = new JLabel(strLbl);
            JTextField line = new JTextField("0.0");
            line.addKeyListener(new TextKeyAdapter());
            line.addFocusListener(new TextFocusListener());
            
            m_outgoing.add(line);
            
            grpBox.add(Box.createVerticalStrut(5));
            itemBox.add(lbl);
            itemBox.add(line);
            grpBox.add(itemBox);
        }
        
        m_inputBox.add(grpBox);
    }
    
    public void initButtons()
    {
        m_btnCaculate = new JButton("计算");
        m_btnReset = new JButton("重置");
        m_btnCaculate.addMouseListener(new MouseAdapter() {
        	public void mousePressed(MouseEvent e) {
        		onBtnCalculatePressed();
        	}
        });
        
        m_btnReset.addMouseListener(new MouseAdapter() {
        	public void mousePressed(MouseEvent e) {
        		onBtnResetPressed();
        	}
        });
    }
    
    public void onBtnResetPressed()
    {
    	Iterator<JTextField> i = m_incoming.iterator();
        while (i.hasNext()) {
        	JTextField line = i.next();
        	line.setText("0.0");
        }

        i = m_outgoing.iterator();
        while (i.hasNext()) {
        	JTextField line = i.next();
        	line.setText("0.0");
        }
    }

    public void onBtnCalculatePressed()
    {
        float incomingPay = 0.0f;
        Iterator<JTextField> i = m_incoming.iterator();
        while (i.hasNext()) {
        	JTextField line = i.next();
        	if (!line.getText().isEmpty()) {
        	    incomingPay += Float.valueOf(line.getText()).floatValue();
        	}
        }

        float outgoingPay = 0.0f;
        i = m_outgoing.iterator();
        while (i.hasNext()) {
        	JTextField line = i.next();
        	if (!line.getText().isEmpty()) {
        	    outgoingPay += Float.valueOf(line.getText()).floatValue();
        	}
        }

        float moneyTmp = ResourceController.instance().calculateRate(incomingPay, outgoingPay);
        if (0 <= moneyTmp) {
        	String text = new String("您所需缴纳的个人所得税为:" + moneyTmp + " 元");
        	JOptionPane.showMessageDialog(this, text, "所需要缴纳的个人所得税", JOptionPane.INFORMATION_MESSAGE);
        }
        else {
        	String text = new String("由于减去扣除项后的余额小于" + ResourceController.instance().getStartMoney() + "，所以您无需缴纳个人所得税！");
        	JOptionPane.showMessageDialog(this, text, "无需缴纳税款", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    private class TextFocusListener implements FocusListener {
    	private static final String m_defaultText = "0.0";
		public void focusGained(FocusEvent e) {
		    JTextField text = (JTextField)e.getSource();
		    if (text.getText().equals(m_defaultText)) {
		    	text.setText("");
		    }
		}
		
		public void focusLost(FocusEvent e) {
			JTextField text = (JTextField)e.getSource();
		    if (text.getText().isEmpty()) {
		    	text.setText(m_defaultText);
		    }
		    else if (text.getText().endsWith(".")) {
		    	text.setText(text.getText() + "0");
		    }
		}
    }
    
    private class TextKeyAdapter extends KeyAdapter {
		public void keyTyped(KeyEvent arg0) {
			JTextField textField = (JTextField)arg0.getSource();
			if ('.' == arg0.getKeyChar()) {
				if (textField.getText().isEmpty()) {
					arg0.consume();
					return;
				}
				if (textField.getText().contains(".")) {
					arg0.consume();
				}
			}
			else {
				if ('0' > arg0.getKeyChar()
						|| '9' < arg0.getKeyChar())
				{
					arg0.consume();
				}
			}
		}
	}
}
